/*
 * @Author: 刘润泽
 * @Date: 2023-02-22 19:39:56
 * @LastEditors: 刘润泽
 * @LastEditTime: 2023-03-14 20:19:35
 * @Description: 
 */
package server.business.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import server.business.pojo.*;
import server.business.service.TransfusionReviewService;
import server.business.mapper.TransfusionReviewMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Galaxy
 * @description 针对表【transfusion_review】的数据库操作Service实现
 * @createDate 2022-11-08 12:46:25
 */
@Service("transfusionReviewService")
public class TransfusionReviewServiceImpl extends ServiceImpl<TransfusionReviewMapper, TransfusionReview>
        implements TransfusionReviewService {
    @Autowired
    private TransfusionReviewMapper transfusionReviewMapper;

    /**
     * 申请同时添加记录
     * 
     * @param transfusionNumber
     */
    @Override
    public void insertRecord(String transfusionNumber) {
        transfusionReviewMapper.insertRecord(transfusionNumber);
    }

    /**
     * 展示临床输血申请审核信息
     * 
     * @return
     */
    @Override
    public Page<TransfusionReview> getClinicPage() {
        Page<TransfusionReview> page = new Page<>(1, 5);
        QueryWrapper<TransfusionReview> wrapper = new QueryWrapper<>();
        wrapper.likeRight(true, "transfusion_number", "C%");
        IPage<TransfusionReview> iPage = transfusionReviewMapper.selectPage(page, wrapper);
        List<TransfusionReview> records = iPage.getRecords();
        for (TransfusionReview record : records) {
            System.out.println(record);
        }
        return page;
    }

    /**
     * 展示自体输血申请状态信息
     * 
     * @return
     */
    @Override
    public Page<TransfusionReview> getAutoInfo() {
        Page<TransfusionReview> page = new Page<>(1, 5);
        QueryWrapper<TransfusionReview> wrapper = new QueryWrapper<>();
        wrapper.likeRight(true, "transfusion_number", "A%");
        IPage<TransfusionReview> iPage = transfusionReviewMapper.selectPage(page, wrapper);
        List<TransfusionReview> records = iPage.getRecords();
        for (TransfusionReview record : records) {
            System.out.println(record);
        }
        return page;
    }

    /**
     * 展示术中申请审核状态信息
     * 
     * @return
     */

    @Override
    public Page<TransfusionReview> getSurgeyInfo() {
        Page<TransfusionReview> page = new Page<>(1, 5);
        QueryWrapper<TransfusionReview> wrapper = new QueryWrapper<>();
        wrapper.likeRight(true, "transfusion_number", "S%");
        IPage<TransfusionReview> iPage = transfusionReviewMapper.selectPage(page, wrapper);
        List<TransfusionReview> records = iPage.getRecords();
        for (TransfusionReview record : records) {
            System.out.println(record);
        }
        return page;
    }

    /**
     * 根据条件搜索临床输血申请
     * 
     * @param currentPage
     * @param pageSize
     * @param transfusionReview
     * @return
     */

    @Override
    @SuppressWarnings("")
    public IPage<TransfusionReview> getSelectClinicPage(int currentPage, int pageSize,
            TransfusionReview transfusionReview, String startTime, String endTime, String reviewStartTime,
            String reviewEndTime) {
        LambdaQueryWrapper<TransfusionReview> queryWrapper = new LambdaQueryWrapper<TransfusionReview>();
        queryWrapper
                .eq(Strings.isNotBlank(transfusionReview.getTransfusionNumber()),
                        TransfusionReview::getTransfusionNumber, transfusionReview.getTransfusionNumber())
                .eq(Strings.isNotBlank(transfusionReview.getState()), TransfusionReview::getState,
                        transfusionReview.getState())
                .likeRight(TransfusionReview::getTransfusionNumber, "C%")
                .ge(startTime != null, TransfusionReview::getApplyTime, startTime)
                .le(endTime != null, TransfusionReview::getApplyTime, endTime)
                .ge(reviewStartTime != null, TransfusionReview::getReviewTime, reviewStartTime)
                .le(reviewEndTime != null, TransfusionReview::getReviewTime, reviewEndTime);
        IPage<TransfusionReview> page = new Page<>(currentPage, pageSize);
        transfusionReviewMapper.selectPage(page, queryWrapper);
        return page;

    }

    /**
     * 按照条件搜索自体输血申请信息
     * 
     * @param currentPage
     * @param pageSize
     * @param transfusionReview
     * @return
     */
    @Override
    @SuppressWarnings("")
    public IPage<TransfusionReview> getSelectAutoPage(int currentPage, int pageSize,
            TransfusionReview transfusionReview, String startTime, String endTime, String reviewStartTime,
            String reviewEndTime) {
        LambdaQueryWrapper<TransfusionReview> queryWrapper = new LambdaQueryWrapper<TransfusionReview>();
        queryWrapper
                .eq(Strings.isNotBlank(transfusionReview.getTransfusionNumber()),
                        TransfusionReview::getTransfusionNumber, transfusionReview.getTransfusionNumber())
                .eq(Strings.isNotBlank(transfusionReview.getState()), TransfusionReview::getState,
                        transfusionReview.getState())
                .likeRight(TransfusionReview::getTransfusionNumber, "A%")
                .ge(startTime != null, TransfusionReview::getApplyTime, startTime)
                .le(endTime != null, TransfusionReview::getApplyTime, endTime)
                .ge(reviewStartTime != null, TransfusionReview::getReviewTime, reviewStartTime)
                .le(reviewEndTime != null, TransfusionReview::getReviewTime, reviewEndTime);
        IPage<TransfusionReview> page = new Page<>(currentPage, pageSize);
        transfusionReviewMapper.selectPage(page, queryWrapper);
        return page;
    }

    /**
     * 按照条件搜索术中输血申请信息
     * 
     * @param currentPage
     * @param pageSize
     * @param transfusionReview
     * @return
     */
    @Override
    @SuppressWarnings("")
    public IPage<TransfusionReview> getSelectSurPage(int currentPage, int pageSize, TransfusionReview transfusionReview,
            String startTime, String endTime, String reviewStartTime, String reviewEndTime) {
        System.out.println("进来了嘛");
        LambdaQueryWrapper<TransfusionReview> queryWrapper = new LambdaQueryWrapper<TransfusionReview>();
        queryWrapper
                .eq(Strings.isNotBlank(transfusionReview.getTransfusionNumber()),
                        TransfusionReview::getTransfusionNumber, transfusionReview.getTransfusionNumber())
                .eq(Strings.isNotBlank(transfusionReview.getState()), TransfusionReview::getState,
                        transfusionReview.getState())
                .likeRight(TransfusionReview::getTransfusionNumber, "S%")
                .ge(startTime != null, TransfusionReview::getApplyTime, startTime)
                .le(endTime != null, TransfusionReview::getApplyTime, endTime)
                .ge(reviewStartTime != null, TransfusionReview::getReviewTime, reviewStartTime)
                .le(reviewEndTime != null, TransfusionReview::getReviewTime, reviewEndTime);
        IPage<TransfusionReview> page = new Page<>(currentPage, pageSize);
        transfusionReviewMapper.selectPage(page, queryWrapper);
        return page;
    }

    /**
     * 更新输血申请审核记录信息
     * 
     * @param transfusionNumber
     * @param transfusionReview
     */
    @Override
    public void updateReview(String transfusionNumber, TransfusionReview transfusionReview) {
        LambdaUpdateWrapper<TransfusionReview> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Strings.isNotBlank(transfusionNumber),
                TransfusionReview::getTransfusionNumber, transfusionNumber);
        System.out.println(transfusionNumber);
        int flag = Integer.parseInt(transfusionReview.getState());
        if (flag == 1) {
            updateWrapper.set(TransfusionReview::getApplyTime, new Date())
                    .set(Strings.isNotBlank(transfusionReview.getApplicantId()),
                            TransfusionReview::getApplicantId, transfusionReview.getApplicantId())
                    .set(Strings.isNotBlank(transfusionReview.getState()),
                            TransfusionReview::getState, transfusionReview.getState());
        } else {
            updateWrapper.set(transfusionReview.getReviewerId() != null,
                    TransfusionReview::getReviewerId, transfusionReview.getReviewerId())
                    .set(TransfusionReview::getReviewTime, new Date())
                    .set(Strings.isNotBlank(transfusionReview.getRejectReason()),
                            TransfusionReview::getRejectReason, transfusionReview.getRejectReason())
                    .set(Strings.isNotBlank(transfusionReview.getState()),
                            TransfusionReview::getState, transfusionReview.getState());
        }

        int result = transfusionReviewMapper.update(null, updateWrapper);
    }

}
